/*******************************************************************************
 *  Control class
 ******************************************************************************/

// example settings object
var settings = {
    id: 'id'
}

/**
 *  Represent base control model
 */
var ControlContainer = function(options) {
    
    // this class should extend an Element class
    $.extend(true, this, new Element($('<div>')))
    
    // ------------------------------------------------------- fields
    this.fields = new Array();
    this.fieldsHolder = new Element($('<div>')).addClass('controls');
    this.labelElement = null;// new Element($('<label>')).setText(options.title).addAttribute('for', options.id).addClass('control-label');
    
    // ------------------------------------------------------- settings
    this.defaults = {
    };
    
    var settings = this.prepareSettings(options);
    settings.id = settings.id + '-control'
    
    // ------------------------------------------------------- methods
    
    /**
     *  Sets control title
     */
    this.setTitle = function(title, id){
        if(title){
            if(!this.labelElement){
                this.labelElement = new Element($('<label>')).addClass('control-label');
                this.getElement().prepend(this.labelElement.getElement());
            }
            this.labelElement
                .setText(title)
                .addAttribute('for', id);
                
            if(this.settings.rules && this.settings.rules.required){
                this.labelElement.getElement().append($('<span style="color:red !important">').text(' *'));
            }
        }
        return this;
    }
    
    this.addElement = function(field){
        field.parent = this;
        this.fieldsHolder.append(field)
        this.fields.push(field);
        return this;
    }
    
    this.getElementByIndex = function(index){
        if(this.fields.length > 0 && index < this.fields.length && index >= 0){
            return this.fields[index];
        }
        return null;
    }
    
    this.getFields = function(){
        return this.fields;
    }
    
    this.makeValid = function(){
        this.addClass('success').removeClass('error');
    }
    
    this.makeInvalid = function(){
        this.removeClass('success').addClass('error');
    }
    
    // ------------------------------------------------------- preparing
    this.setId(settings.id)
        .addClass('control-group')
        .setTitle(settings.title, settings.id)
        .append(this.fieldsHolder); // inits holder
    
    console.log('ControlContainer was created with id = '+this.getId());
}